import { defineStore } from 'pinia'

export const useShopStore = defineStore('shopStore', {
  state: () => ({
    pageParams: {},
    scrollTop: {}
  }),
  actions: {
    savePageParams (name, params) {
      this.pageParams[name] = params;
    },
    getPageParams (name) {
      return this.pageParams[name];
    },
    saveScrollTop (name) {
      this.scrollTop[name] = document.documentElement.scrollTop;
    },
    restoreScrollTop (name) {
      const top = this.scrollTop[name] || 0;
      const timer = setInterval(() => {
        if (document.readyState === 'complete') {
          setTimeout(() => {
            window.scrollTo(0, top);
          }, 500);

          window.clearInterval(timer)
        }
      }, 100);
    }
  }
})
